package com.liu.springmvc.view;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author LIU
 * @version 1.0
 * @date 2024/5/16 17:43
 * @description TODO
 */

public class MyView implements View {
   private File viewFile;

   public MyView(File viewFile) {
      this.viewFile = viewFile;
   }

   @Override
   public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
      //无反射，不框架
      //无正则，不架构

      StringBuffer sb = new StringBuffer();
      RandomAccessFile ra = new RandomAccessFile(this.viewFile,"r");

      String line = null;
      while (null != (line = ra.readLine())){
         line = new String(line.getBytes("iso-8859-1"),"utf-8");

         Pattern pattern = Pattern.compile("￥\\{[^\\}]+\\}",Pattern.CASE_INSENSITIVE);
         Matcher matcher = pattern.matcher(line);
         while (matcher.find()){
            //  ￥{teacher}
            String paramName = matcher.group();

            paramName = paramName.replaceAll("￥\\{|\\}","");
            Object paramValue = model.get(paramName);
            if(null == paramValue){continue;}
            line = matcher.replaceFirst(makeStringForRegExp(paramValue.toString()));
            matcher = pattern.matcher(line);
         }
         sb.append(line);
      }

      response.setCharacterEncoding("utf-8");
      response.getWriter().write(sb.toString());
   }


   //处理特殊字符
   public static String makeStringForRegExp(String str) {
      return str.replace("\\", "\\\\").replace("*", "\\*")
              .replace("+", "\\+").replace("|", "\\|")
              .replace("{", "\\{").replace("}", "\\}")
              .replace("(", "\\(").replace(")", "\\)")
              .replace("^", "\\^").replace("$", "\\$")
              .replace("[", "\\[").replace("]", "\\]")
              .replace("?", "\\?").replace(",", "\\,")
              .replace(".", "\\.").replace("&", "\\&");
   }
}
